<?php
	header('content-type:text/html;charset=utf-8');
	date_default_timezone_set("Asia/Chongqing");
	require '../Class/autoload.php';





	//===== ### 验证token

	$token_arr = array(		// 跳过token验证的type项 添加到这里

	);
	if(!in_array($_REQUEST['type'],$token_arr)){
		//验证远程token
		if($_REQUEST['r'] != 'a'){
			exit('10002');	//远程token 验证失败
		}
	}


	switch ($_REQUEST['type']) {

		//从全局库或upyun获取已开通城市数据 Gets(..)格式
		case 'GetOpenCityInfo':
			require_once('../Class/UpYun/vendor/autoload.php');
			$serviceConfig = new \Upyun\Config($config['upyun_bucketname'],$config['upyun_username'],$config['upyun_password']);
			$up = new \Upyun\Upyun($serviceConfig);
			$path = '/CityInfo/city_open.html';

			if($up->has($path)){
				$ourl = $config['upyun_src'].$path;
				$res = CurlForGet($ourl);
				if(preg_match('/^Gets\({.*}\)$/',$res)){
					exit($res);
				}
			}

			$pdo = MyPdoShare::init();
			$sql = 'select id,code,name,parent_id,level,db_host,db_name from share_city';
			//$sql = 'select * from share_city limit 1';
			$res = $pdo->dql($sql,array());
			if($res){
				$province = $province2 = $city = $city2 = $county2 = array();
				foreach($res as $v){
					if($v['level'] == 0){
						$province[] = $v;
					}
					if($v['level'] == 1){
						$city[] = $v;
					}
					if($v['level'] == 2){
						if($v['db_host'] != '' && $v['db_name'] != ''){
							$county2[] = $v;
						}
					}
				}

				if(count($county2) < 1){
					exit;
				}

				foreach($county2 as $v_county){
					foreach($city as $v_city){
						if($v_city['id'] == $v_county['parent_id']){
							$city2[] = $v_city;
						}
					}
				}

				foreach($city2 as $v_city){
					foreach($province as $v_province){
						if($v_province['id'] == $v_city['parent_id']){
							$province2[] = $v_province;
						}
					}
				}


				$county3 = json_encode($county2);
				$city3 = json_encode($city2);
				$province3 = json_encode($province2);
				$FullCityStr = '{"province":'.$province3.',"city":'.$city3.',"county":'.$county3.'}';

				$FullCityStr2 = 'Gets('.$FullCityStr.')';
				echo $FullCityStr2;
				$up->write($path,$FullCityStr2);
			}
			break;



		default :
			echo 'default';
			break;

	}





